package com.dsg.service.set.impl;

import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.dao.set.DsgSetCouponReturnMapper;
import com.dsg.entity.coupon.DsgCouponReturn;
import com.dsg.entity.set.DsgSetCouponReturn;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.coupon.IDsgCouponReturnService;
import com.dsg.service.set.IDsgSetCouponReturnService;
import com.dsg.util.SnowflakeKeyWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 油费回赠设置 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-11-24
 */
@Service
public class DsgSetCouponReturnServiceImpl extends DsgServiceImpl<DsgSetCouponReturnMapper, DsgSetCouponReturn> implements IDsgSetCouponReturnService {

    @Autowired
    private IDsgCouponReturnService dsgCouponReturnService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDsgSetCouponReturn(DsgSetCouponReturn dsgSetCouponReturn) {
        //回赠金额
        BigDecimal retAmount = BigDecimal.ZERO;
        dsgSetCouponReturn.setOid(UUIDGenerator.getUUID());
        Date date = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        List<DsgCouponReturn> dsgCouponReturnList = new ArrayList<>();
//        String entCode = SingletonLoginUtils.getSystemEntCode();
        for (int i = 0; i < dsgSetCouponReturn.getGrantNum(); i++) {
            retAmount = dsgSetCouponReturn.getReturnMoney().divide(new BigDecimal(dsgSetCouponReturn.getGrantNum()));
            DsgCouponReturn dsgCouponReturn = new DsgCouponReturn();
            dsgCouponReturn.setOid(UUIDGenerator.getUUID());
            dsgCouponReturn.setSetId(dsgSetCouponReturn.getOid());
            dsgCouponReturn.setCouponCode(SnowflakeKeyWorker.nextStrId());
            dsgCouponReturn.setCusNo(dsgSetCouponReturn.getCusNo());
            dsgCouponReturn.setRetAmount(retAmount);
            dsgCouponReturn.setBalAmount(retAmount);
            dsgCouponReturn.setEffDate(rightNow.getTime());
            dsgCouponReturn.setRemarks(dsgSetCouponReturn.getRemarks());
            dsgCouponReturn.setVerify(0);
            rightNow.add(Calendar.DATE, dsgSetCouponReturn.getEffeDura());
            dsgCouponReturn.setExpDate(rightNow.getTime());
            switch (dsgSetCouponReturn.getStageUnit()) {
                case "1":
                    rightNow.add(Calendar.DATE, dsgSetCouponReturn.getEachInt() * 7);
                    break;
                case "2":
                    rightNow.add(Calendar.MONTH, dsgSetCouponReturn.getEachInt());
                    break;
            }
            dsgCouponReturnList.add(dsgCouponReturn);
        }
        if (CollectionUtil.isNotEmpty(dsgCouponReturnList)){
            baseMapper.insert(dsgSetCouponReturn);
            dsgCouponReturnService.saveBatch(dsgCouponReturnList);
        }
    }
}
